#ifndef T_PAINTWIDGET_H
#define T_PAINTWIDGET_H
#include <QWidget>
#include <QJsonObject>
#include <QJsonArray>
#include "common.h"

class QGraphicsView;
class T_GridScene;
class T_CustomRectItem;
class ElaContentDialog;
class T_Cube;
class ElaPushButton;

#define T_PaintWidgetIns T_PaintWidget::Instance()

class T_PaintWidget : public QWidget {
    Q_OBJECT

public:
    explicit T_PaintWidget(QWidget *parent = nullptr);
    ~T_PaintWidget();

    static T_PaintWidget* Instance();

public:
    void ChangeGridVisible(bool is_visible);
    void UpdateLineTip();
    void ExportImage();
    QImage GetImg();
    void NewFile();
    void ClearPaint();
    void SaveFile();
    void OpenFile();
    void ResizeView();
    void ResizeView1();

private:
    //整体初始化
    void initialze();
    //初始化视图窗口
    void initView();
    void initView1();

    //清除场景组件
    void clearScene();
    void clearPaint();
    // 处理custom_array项（无嵌套）
    QJsonArray processCustomItems(T_CustomRectItem* parent);
    // 处理group_array项（无嵌套）
    QJsonArray processGroupItems(T_CustomRectItem* parent);
    // 处理outer_array项（完整对象）
    QJsonArray processOuterItems();
    // 获取outer项
    QList<T_CustomRectItem*> getOuterItems();

    // 获取custom_array项
    void praseCustomItems(const QJsonObject &obj);
    // 获取group_array项
    void praseGroupItems(const QJsonObject &obj);
    // 获取outer_array项
    void praseOuterItems(const QJsonObject &obj);

private slots:
    void on_AddMainRect(const cutomitem& data,qreal leftMar = 30.0,qreal topMar = 30.0,qreal rightMar = 30.0,qreal bottomMar = 30.0);
    void on_AddMainRect1(const cutomitem& data,qreal leftMar = 30.0,qreal topMar = 30.0,qreal rightMar = 30.0,qreal bottomMar = 30.0);
    void on_AddSubRect(const cutomitem& data);
    void on_AddBlockRect(const cutomitem& data,double space,int num,int direction);
    void on_AddBlockRect1(const cutomitem& data,double space,int num,int direction);
    void on_AddBlockRect_import(const cutomitem& data,double space,int num,int direction
                                ,qreal leftMar,qreal topMar,qreal rightMar,qreal bottomMar);
    void on_AddOuterRect(const cutomitem& data,qreal leftMar,qreal topMar,qreal rightMar,qreal bottomMar
                        ,const QJsonArray& custom_array,const QJsonArray& group_array);

    void on_addOuterRect_paintLine(const QStringList &tmpList);
    void on_addOuterRect_paintLine1(const QStringList &tmpList);

private:
    ElaPushButton *m_tbtn_export_excel{nullptr};

    QGraphicsView* m_graphics_view{nullptr};
    T_GridScene* m_grid_scene{nullptr};

    QGraphicsView* m_graphics_view1{nullptr};
    T_GridScene* m_grid_scene1{nullptr};

    T_CustomRectItem* m_main_rect{nullptr};
    T_CustomRectItem* m_main_rect1{nullptr};
    ElaContentDialog* m_tip_dialog{nullptr};
    ElaContentDialog* m_clear_dialog{nullptr};

    bool m_is_import{false};

    qreal outer_min_x_diff = 65535.0;
    qreal outer_min_y_diff = 65535.0;

    // QWidget interface
protected:
    void resizeEvent(QResizeEvent *event);
};
#endif
